package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.tmall.com.exception.SystemException;
import com.cts.tmall.com.message.RspCode;
import com.cts.tmall.dal.entity.TmallLimitSale;
import com.cts.tmall.dal.mapper.TmallLimitSaleMapper;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class LimitSaleService {

    @Autowired
    private TmallLimitSaleMapper limitSaleMapper;

    public Map<String, Object> queryLimitSale(Long createUserId, String eventName, String status, String discountType,
                                              Long pageNumb, Long pageSize) {
        LambdaQueryWrapper<TmallLimitSale> wrapper = Wrappers.<TmallLimitSale>lambdaQuery()
                .eq(TmallLimitSale::getCreateUserId, createUserId)
                .eq(StringUtils.isNotBlank(eventName), TmallLimitSale::getEventName, eventName)
                .eq(StringUtils.isNotBlank(status), TmallLimitSale::getStatus, status)
                .eq(StringUtils.isNotBlank(discountType), TmallLimitSale::getDiscountType, discountType);

        Page<TmallLimitSale> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNumb);
        page = limitSaleMapper.selectPage(page, wrapper);
        return ImmutableMap.<String, Object>builder()
                .put("pageNumb", page.getCurrent())
                .put("pageSize", page.getSize())
                .put("total", page.getTotal())
                .put("data", page.getRecords())
                .build();
    }

    public TmallLimitSale addLimitSale(TmallLimitSale limitSale) {
        if (StringUtils.isBlank(limitSale.getEventName())) {
            log.error("活动标题为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "活动标题为空！");
        }
        if (StringUtils.isBlank(limitSale.getEventDesc())) {
            log.error("活动描述为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "活动描述为空！");
        }
        if (StringUtils.isBlank(limitSale.getNeedPreHeat())) {
            log.error("是否预热为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "是否预热为空！");
        }
        if (StringUtils.isBlank(limitSale.getDiscountType())) {
            log.error("优惠类型为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "优惠类型为空!");
        }
        if (StringUtils.isBlank(limitSale.getOrderCancelTime())) {
            log.error("订单取消时间为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "订单取消时间为空！");
        }
        limitSale.setStatus("01");
        limitSaleMapper.insert(limitSale);
        return limitSaleMapper.selectById(limitSale.getEventNo());
    }

    public TmallLimitSale updLimitSale(TmallLimitSale limitSale) {
        if (limitSale.getEventNo() == null) {
            log.error("活动编号为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "活动编号为空！");
        }
        limitSaleMapper.updateById(limitSale);
        return limitSaleMapper.selectById(limitSale.getEventNo());
    }
}
